package com.wu.springboot_study.aspect;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.wu.springboot_study.util.JedisUtil;
import lombok.AllArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author benjamin_5
 * @Description
 * @date 2022/12/5
 */
@Component
@Aspect
@AllArgsConstructor
public class OnlyCallOnceAspect {

    private final JedisUtil jedisUtil;

    @Pointcut(value = "@annotation(com.wu.springboot_study.annotion.OnlyCallOnce)")
    public void callOnce(){

    }

    @Before(value = "callOnce()")
    public void before(JoinPoint joinPoint) throws Exception {
        String name = joinPoint.getSignature().getName();
        boolean b = jedisUtil.tryLock(name, 10000);
        if(!b){
            throw new Exception("方法调用中，请等候");
        }
        System.out.println("执行前的去重处理");
    }

    @After(value = "callOnce()")
    public void after(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("方法名："+name);
        jedisUtil.releaseLock(name);
        System.out.println("执行后的处理");
    }

}
